<template>
  <addFileView ref="addFileView" />
  <addEditBaseAchievement ref="addEditBaseAchievement" />
  <div class="app-table">
    <div class="app-body">
      <div class="app-search">
        <el-form :inline="true" class="demo-form-inline" v-model="form">
          <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId">
            <el-select clearable v-model="form.gradeId" autocomplete="off" @change="filterGradeId" style="width: 192px;"
              placeholder="请选择所在年级">
              <el-option v-for="gradeId in gradeIds" :key="gradeId" :value="gradeId"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="所在学年" :label-width="formLabelWidth" prop="classId">
            <el-select clearable v-model="form.schoolYear" autocomplete="off" placeholder="请选择所在学年"
              style="width: 192px;">
              <el-option v-for="item in schoolYears" :key="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="考试名称" :label-width="formLabelWidth" prop="classId">
            <el-select clearable v-model="form.typeName" autocomplete="off" placeholder="请选择考试名称" style="width: 192px;">
              <el-option v-for="item in types" :key="item.name" :value="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所在班级" :label-width="formLabelWidth" prop="classId">
            <el-select clearable v-model="form.classId" autocomplete="off" placeholder="请选择所在班级" style="width: 192px;">
              <el-option v-for="item in classInfos" :key="item.classId" :value="item.classId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="学生姓名">
            <el-input placeholder="请输入学生姓名" v-model="form.studentName"></el-input>
          </el-form-item>
          <el-form-item label="学生学号">
            <el-input placeholder="请输入学生学号" v-model="form.studentNumber"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="serach_btn" type="primary" plain @click="onSubmit"
              @keyup.enter="searchEnter">搜索</el-button>

            <el-button class="serach_btn" type="success" plain @click="importExcel">按年级导入<el-icon
                class="el-icon--right">
                <Upload />
              </el-icon></el-button>
            <!-- <el-button type="info" plain @click="importEditExcel"
              >按班级导入<el-icon class="el-icon--right">
                <Upload /> </el-icon
            ></el-button> -->
            <el-button class="serach_btn" type="warning" plain @click="exportExcel"
              v-loading.fullscreen.lock="fullscreenLoading">导出报表</el-button>
            <el-button class="serach_btn" type="info" plain @click="calcRepeat">重算排名</el-button>
            <el-button class="batch_del_btn" type="danger" plain @click="deleteAll">批量删除</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table size="default" :data="list" :row-class-name="tableRowClassName">
        <el-table-column prop="index" label="序号" width="60">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="schoolYear" label="学年" width="100"/> -->
        <el-table-column prop="name" label="考试名称" width="190" />
        <el-table-column prop="clsName" label="班级" width="120" />
        <el-table-column prop="studentName" label="姓名" width="90" />
        <el-table-column prop="studentNumber" label="学号" width="90" />
        <el-table-column prop="chinese" label="语文｜数学｜英语" width="180">
          <template #default="scope">
            <span class="score" style="white-space: nowrap;">{{ scope.row.chinese }}</span> |
            <span class="score" style="white-space: nowrap;">{{ scope.row.mathematics }} </span>|
            <span class="score" style="white-space: nowrap;">{{ scope.row.english }} </span></template>
        </el-table-column>
        <el-table-column prop="chinese" label="物理｜化学｜生物" width="180">
          <template #default="scope">
            <span class="score" style="white-space: nowrap;">{{ scope.row.physics }}</span> |
            <span class="score" style="white-space: nowrap;">{{ scope.row.chemistry }}</span> |
            <span class="score" style="white-space: nowrap;">{{ scope.row.biology }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="chinese" label="政治｜历史｜地理" width="180">
          <template #default="scope">
            <span class="score" style="white-space: nowrap;">{{ scope.row.politics }} </span>|
            <span class="score" style="white-space: nowrap;">{{ scope.row.history }} </span>|
            <span class="score" style="white-space: nowrap;">{{ scope.row.geography }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template style="display: block" #default="scope">
            <el-button class="edit_btn" size="small" type="success" @click="editbaseAchievement(scope.row)">编辑
            </el-button>
            <el-button class="del_btn" size="small" type="danger" @click="deletebaseAchievement(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
        <template v-slot:empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
      </el-table>
      <el-config-provider :locale="locale">
        <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
          @current-change="changeCurrentPage">
        </el-pagination>
      </el-config-provider>
    </div>
    <bottomView></bottomView>
  </div>
</template>
<script>
import bottomView from "@/components/bottom.vue";
import addFileView from "@/components/baseInfo/addBaseAchievementExcel.vue";
import addEditBaseAchievement from "@/components/baseInfo/addEditBaseAchievement.vue";
import {
  ElConfigProvider,
  ElMessageBox,
  ElMessage,
  ElLoading,
} from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";

export default {
  name: "baseAchievementListView",
  data() {
    return {
      type: 0,
      title: "",
      gradeIds: [],
      types: [],
      page: 0,
      pageSize: 10,
      total: 0,
      limit: 10,
      fullscreenLoading: false,
      list: [],
      schoolYears: [],
      map: {},
      classInfos: [],
      form: {
        studentNumber: "",
        classId: "",
        gradeId: "",
        password: "",
        studentName: "",
        examineNumber: "",
        idCard: "",
        fatherMobile: "",
        motherMobile: "",
        fatherName: "",
        motherName: "",
        address: "",
        fatherWorkUnit: "",
        motherWorkUnit: "",
        mobile: "",
        alias: "",
        photo: "",
        politicalOutlook: "",
        incomeDate: "",
        nation: "",
        graduateSchool: "",
        admissionScore: "",
        type: "",
        incomeCode: "",
        beforeName: "",
        nationality: "",
        modeStudy: "",
        oneCardNumber: "",
        schoolYear: "",
        typeName: "",
      },
    };
  },
  components: {
    addFileView,
    ElLoading,
    bottomView,
    addEditBaseAchievement,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.getGradeIds();
    this.getBaseClassInfoList();
    this.getbaseAchievementList();

  },
  watch: {
    $route() {
      this.getGradeIds();
      this.getBaseClassInfoList();
      this.getbaseAchievementList();
    },
  },
  methods: {
    getBaseClassInfoList() {
      httpService("/javaApi/baseClassInfo/getAllEffectClass", {}, "get").then(
        (data) => {
          this.map = data;
        }
      );
    },
    filterGradeId(val) {
      this.classInfos = this.map[val];
    },
    getGradeIds() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth() + 1);
      let gradeIds = [];
      if (nowMonth > 6) {
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
        gradeIds.push(nowYear);
      } else {
        gradeIds.push(nowYear - 3);
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
      }
      this.gradeIds = gradeIds;
      this.form.gradeId = gradeIds[0]
      this.getSchoolYears();
      this.getAchievementTypeList();
    },
    getAchievementTypeList() {
      httpService("/javaApi/achievementType/list/1/1000", {}, "get").then(
        (data) => {
          this.types = data.list;
        }
      );
    },
    getSchoolYears() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth());
      let schoolYears = [];
      if (nowMonth > 6) {
        // 2023
        schoolYears.push(nowYear - 2000 + "-" + (nowYear - 1999) + "学年上");

        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年下");
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年上");

        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年下");
        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年上");
      } else {
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年下");
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年上");

        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年下");
        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年上");

        schoolYears.push(nowYear - 2003 + "-" + (nowYear - 2002) + "学年下");
        schoolYears.push(nowYear - 2003 + "-" + (nowYear - 2002) + "学年上");
      }
      this.schoolYears = schoolYears;
      this.form.schoolYear = schoolYears[0]
    },


    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getbaseAchievementList();
      }
    },
    add() {
      this.$refs.addEditBaseAchievement.title = "添加学生";
      this.$refs.addEditBaseAchievement.dialogFormVisible = true;
    },
    editbaseAchievement(obj) {
      this.$refs.addEditBaseAchievement.title = "修改信息";
      this.$refs.addEditBaseAchievement.getbaseAchievementById(obj.id);
      this.$refs.addEditBaseAchievement.dialogFormVisible = true;
    },
    deletebaseAchievement(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/baseAchievement/deleteById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getbaseAchievementList();
            } else {
              ElMessage.error("删除失败，请重试！");
            }
          });
        })
        .catch(() => { });
    },
    calcRepeat() {
      if (this.form.gradeId == "") {
        ElMessage.error("请选择年级后计算");
        return;
      }
      if (this.form.schoolYear == "") {
        ElMessage.error("请选择学年后计算");
        return;
      }
      if (this.form.typeName == "") {
        ElMessage.error("请选择考试名称后计算");
        return;
      }

      ElMessageBox.confirm("确定要重算排名吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
          params.append("gradeid", this.form.gradeId);
          params.append("typeName", this.form.typeName);
          params.append("schoolYear", this.form.schoolYear);
          params.append("classid", this.form.classId);
          httpService(
            "/javaApi/baseAchievement/calcRepeat",
            params,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.object);
              this.getbaseAchievementList();
            }
          });
        })
        .catch(() => { });
    },
    deleteAll() {
      if (this.form.gradeId == "") {
        ElMessage.error("请选择年级后删除");
        return;
      }
      if (this.form.schoolYear == "") {
        ElMessage.error("请选择学年后删除");
        return;
      }
      if (this.form.typeName == "") {
        ElMessage.error("请选择考试名称后删除");
        return;
      }

      ElMessageBox.confirm("确定要按照筛选条件批量删除吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
          params.append("gradeid", this.form.gradeId);
          params.append("typeName", this.form.typeName);
          params.append("schoolYear", this.form.schoolYear);
          params.append("classid", this.form.classId);

          httpService(
            "/javaApi/baseAchievement/deleteByParams",
            params,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.getbaseAchievementList();
            }
          });
        })
        .catch(() => { });
    },
    reloadData() {
      this.page = 1;
      this.getbaseAchievementList();
    },
    importExcel() {
      // console.log(123,this.types);
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.gradeIds = this.gradeIds;
      this.$refs.addFileView.types = this.types;
      this.$refs.addFileView.schoolYears = this.schoolYears;
      this.$refs.addFileView.title = "按年级导入成绩";
      this.$refs.addFileView.uploadApiUrl =
        "/javaApi/baseAchievement/importExcel";
    },
    // importEditExcel() {
    //   this.$refs.addFileView.dialogFormVisible = true;
    //   this.$refs.addFileView.title = "按班级导入成绩";
    //   this.$refs.addFileView.uploadApiUrl =
    //     "/javaApi/baseAchievement/importEditExcel";
    // },
    exportExcel() {
      this.fullscreenLoading = true;
      httpService(
        "/javaApi/baseAchievement/exportBaseAchievement",
        {
          gradeid: this.form.gradeId,
          classid: this.form.classId,
          studentNumber: this.form.studentNumber,
          studentName: this.form.studentName,
          typeName: this.form.typeName,
          schoolYear: this.form.schoolYear,
        },
        "get"
      ).then((data) => {
        this.fullscreenLoading = false;
        if (data.code == 200) {
          data.message = data.message.replace("http://wx.zhengzhong.cn/javaApi", 'https://wx.zhengzhong.cn/')
          window.location.href = data.message
          // window.open(data.message, "_blank");
        }
      });
    },
    onSubmit() {
      this.getbaseAchievementList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getbaseAchievementList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getbaseAchievementList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getbaseAchievementList() {
      this.form.gradeId ? this.form.gradeId : this.form.gradeId = ''
      this.form.classId ? this.form.classId : this.form.classId = ''
      this.form.studentNumber ? this.form.studentNumber : this.form.studentNumber = ''
      this.form.studentName ? this.form.studentName : this.form.studentName = ''
      this.form.typeName ? this.form.typeName : this.form.typeName = ''
      this.form.schoolYear ? this.form.schoolYear : this.form.schoolYear = ''
      httpService(
        "/javaApi/baseAchievement/list/" + this.page + "/" + this.pageSize,
        {
          gradeid: this.form.gradeId,
          classid: this.form.classId,
          studentNumber: this.form.studentNumber,
          studentName: this.form.studentName,
          typeName: this.form.typeName,
          schoolYear: this.form.schoolYear,
        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
  },
};
</script>
<style scoped>
.score {
  width: 30px;
  text-align: center;
  display: inline-block;
}

.el-main {
  padding: 0px;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 5px 10px 10px;
  /* width: 100%; */
}

.app-search {}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 204px);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}

.serach_btn {
  background-color: #007EFF;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.add_btn {
  background-color: #00c78c;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.export_btn {
  background-color: #FF7200;
  border-radius: 8px;
  color: #fff;
  border: none;
}

.batch_del_btn {
  background-color: #FF0000;
  border-radius: 8px;
  color: #fff;
  border: none;
}

.Ranking_btn {
  background-color: #FF0000;
  border-radius: 8px;
  color: #fff;
  border: none;
}

.edit_btn {
  background-color: #fff;
  color: #007EFF;
  border: none;
}

.del_btn {
  background-color: #fff;
  color: #FF0000;
  border: none;
}
</style>
